﻿using LuShanTea.IService;
using Qiniu.Common;
using Qiniu.Http;
using Qiniu.IO;
using Qiniu.IO.Model;
using Qiniu.Util;
using System;
using System.IO;
using System.Web.Mvc;

namespace LuShanTea.Web.Common
{

    public class UploadFile
    {
        private ISettingService _settingService = DependencyResolver.Current.GetService<ISettingService>();

        private QiNiuSetting _setting;

        /// <summary>
        /// 使用项目中默认的七牛云配置
        /// </summary>
        public UploadFile()
        {
            this._setting = GetQiNiuSetting();
            CheckedQNSetting(_setting);
        }

        /// <summary>
        /// 不使用项目中默认的配置，自己传入七牛云配置信息
        /// </summary>
        /// <param name="settings">七牛云配置信息</param>
        public UploadFile(QiNiuSetting settings)
        {
            this._setting = settings;
            CheckedQNSetting(_setting);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="saveKey"></param>
        /// <param name="inputStream"></param>
        public AjaxResult UploadImg(string saveKey, Stream inputStream)
        {
            Mac mac = new Mac(_setting.AccessKey, _setting.SecretKey);

            // 存储空间名
            string Bucket = _setting.Bucket;

            // 设置上传策略，详见：https://developer.qiniu.com/kodo/manual/1206/put-policy
            PutPolicy putPolicy = new PutPolicy();

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖)，请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;
            putPolicy.Scope = _setting.Bucket + ":" + saveKey;
            putPolicy.SetExpires(3600);

            //只允许上传图片类型
            putPolicy.MimeLimit = "image/*";

            string jstr = putPolicy.ToJsonString();
            string token = Auth.CreateUploadToken(mac, jstr);

            // 设置上传区域
            Config.SetZone(ZoneID.CN_South, false);

            // 表单上传
            FormUploader fu = new FormUploader();
            HttpResult result = fu.UploadStream(inputStream, saveKey, token);

            AjaxResult ajaxResult = new AjaxResult();

            if (result.Code != 200)
            {
                ajaxResult.Status = "error";
                ajaxResult.ErrorMsg = result.Code + result.Text;

                return ajaxResult;
            }


            ajaxResult.Status = "ok";

            return ajaxResult;
        }
        
        /// <summary>
        /// 获取项目中默认的配置
        /// </summary>
        /// <returns></returns>
        private QiNiuSetting GetQiNiuSetting()
        {
            QiNiuSetting setting = new QiNiuSetting();

            setting.AccessKey = _settingService.GetSettingByKey("qiniuAccess").Value;

            setting.SecretKey = _settingService.GetSettingByKey("qiniuSecretKey").Value;

            setting.Bucket = "lushantea";

            return setting;
        }


        /// <summary>
        /// 检查七牛云配置
        /// </summary>
        /// <param name="setting"></param>
        private void CheckedQNSetting(QiNiuSetting setting)
        {
            if (string.IsNullOrEmpty(setting.AccessKey))
            {
                throw new ArgumentException("AccessKey不能为空");
            }

            if (string.IsNullOrEmpty(setting.SecretKey))
            {
                throw new ArgumentException("SecretKey不能为空");
            }

            if (string.IsNullOrEmpty(setting.Bucket))
            {
                throw new ArgumentException("SecretKey不能为空");
            }
        }

    }


}
